<?php

class NewsController extends \BaseController {

	/**
	 * Display a listing of news
	 *
	 * @return Response
	 */
	public function index()
	{
		$news = News::paginate(15);

		return View::make('news.index', compact('news'));
	}

	/**
	 * Show the form for creating a new news
	 *
	 * @return Response
	 */
	public function create()
	{
		return View::make('news.create');
	}

	/**
	 * Store a newly created news in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		$validator = Validator::make($data = Input::all(), News::$rules);

		if ($validator->fails())
		{
			return Redirect::back()->withErrors($validator)->withInput();
		}

        $news = new News;
        $news->title = Input::get('title');
        $news->intro = Input::get('intro');
        $news->content = Input::get('content');
        $news->status = Input::get('status');
        if(Input::hasFile('thumbnail')){
            $uploadfile = Input::file('thumbnail');
            $filename = time().'.news-'.$uploadfile->getClientOriginalExtension();
            $upload_path = '/img/news/';
            $uploadSuccess = $uploadfile->move(public_path().$upload_path, $filename);
            if($uploadSuccess)
                $news->thumbnail = $upload_path.$filename;
            else
                return Redirect::back()->with('error', '上传失败');
        }
        $news->save();

		return Redirect::route('admin.news.index')->with('message', '添加成功');
	}

	/**
	 * Display the specified news.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		$news = News::findOrFail($id);

		return View::make('news.show', compact('news'));
	}

	/**
	 * Show the form for editing the specified news.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		$news = News::find($id);

		return View::make('news.edit', compact('news'));
	}

	/**
	 * Update the specified news in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		$news = News::findOrFail($id);

		$validator = Validator::make($data = Input::all(), News::$rules);

		if ($validator->fails())
		{
			return Redirect::back()->withErrors($validator)->withInput();
		}

        $news->title = Input::get('title');
        $news->intro = Input::get('intro');
        $news->content = Input::get('content');
        $news->status = Input::get('status');
        if(Input::hasFile('thumbnail')){
            $uploadfile = Input::file('thumbnail');
            $filename = time().'.news-'.$uploadfile->getClientOriginalExtension();
            $upload_path = '/img/news/';
            $uploadSuccess = $uploadfile->move(public_path().$upload_path, $filename);
            if($uploadSuccess)
                $news->thumbnail = $upload_path.$filename;
            else
                return Redirect::back()->with('error', '上传失败');
        }
        $news->update();

		return Redirect::route('admin.news.index')->with('message', '编辑成功');
	}

	/**
	 * Remove the specified news from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		News::destroy($id);

		return Redirect::route('news.index');
	}

}
